package cn.tedu.imop.basic.service.impl;

import cn.tedu.imop.api.domain.Attach;
import cn.tedu.imop.api.service.AttachRemoteService;
import cn.tedu.imop.basic.dao.repository.BusinessRepository;
import cn.tedu.imop.basic.entity.Business;
import cn.tedu.imop.basic.service.BusinessService;
import cn.tedu.imop.common.pojo.PageData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

@Transactional(rollbackFor = Exception.class)
@Service
public class BusinessServiceImpl implements BusinessService {

    private BusinessRepository businessRepository;
    private AttachRemoteService attachRemoteService;
    public BusinessServiceImpl(BusinessRepository businessRepository,
                               AttachRemoteService attachRemoteService){
        this.businessRepository = businessRepository;
        this.attachRemoteService=attachRemoteService;
    }
    @Override
    public PageData<Business> getBusinessList(Business business) {
        return businessRepository.selectBusinessList(business);
    }

    @Override
    public Boolean createBusiness(Business business) {
        //保存商家基础信息
        businessRepository.insertBusiness(business);
        //调用远程服务，将营业执照信息和商家信息绑定在一起
        ArrayList<Attach> list=new ArrayList<>();
        String attachIds=business.getAttachIds();
        String [] attachIdArray=attachIds.split(",");
        for(String attachId:attachIdArray) {
            Attach attach = new Attach();
            attach.setId(Long.valueOf(attachId));
            attach.setServiceId(business.getId());
            attach.setServiceType(20);//20表示是商家营业执照
            list.add(attach);
        }
        bindImages(list);
        return true;
    }
    private void bindImages(ArrayList<Attach> list){
         System.out.println("businessServiceImpl:"+list);
         attachRemoteService.batchUpdateBusiness(list);
    }

}
